package spe.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Properties;

/**
 * @author: Dao Anh Vu - May 28, 2012
 * @description: 
 * 		This class load text from rcp.ui.properties to the RCP UI
 * */

public class RCPUIText 
{
	private static String RCP_UI_FILE = "rcp.ui.properties";
	private static String executionPath = null;
	private static Properties texts = null;
	
	public RCPUIText()
	{
	}
	
	public static void load() throws IOException
	{
		if(texts == null)
		{
			texts = new Properties();
			texts.load(new FileInputStream(new File(String.format("%s%s", getExecutionPathWithoutJar(), RCP_UI_FILE))));
		}
	}
	
	public static String getText(String key)
	{
		return texts.getProperty(key);
	}
	
	public static String getExecutionPathWithJar()
	{
		if(executionPath==null || executionPath.trim().equals(""))
		{
			String path = RCPUIText.class.getProtectionDomain().getCodeSource().getLocation().getPath();
			File f = new File(path);
			executionPath = f.getParentFile().getPath();
		}
		return executionPath;
	}
	
	public static String getExecutionPathWithoutJar()
	{
		if(executionPath==null || executionPath.trim().equals(""))
		{
			String path = RCPUIText.class.getProtectionDomain().getCodeSource().getLocation().getPath();
			try {
				executionPath = URLDecoder.decode(path, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return executionPath;
	}
}
